﻿using System;
using System.IO;
using System.Text;
using System.Collections.Generic;

namespace SolutionGeneratorHelper
{
    public static class ENUM_STR_PROJECT
    {
        public static readonly string FIRSTPASS = "Assembly-CSharp-firstpass";
        public static readonly string DEFAULT   = "Assembly-CSharp";
        public static readonly string EDITOR    = "Assembly-CSharp-Editor";
    }

    public static class UnitySolutionHelper
    {
        private static List<List<string>> m_projectConfig = new List<List<string>>();
        public static List<List<string>> ProjectConfig { get => m_projectConfig; set => m_projectConfig = value; }


        public static void Init(List<List<string>> _inList = null)
        {
            UnitySolution.InitFileHandler();
            if (null != _inList)
                SetExtendProjectList(_inList);
        }

        public static void SetExtendProjectList(List<List<string>> _inList)
        {
            foreach(List<string> item in _inList)
            {
                CheckExtendProjectList(item);
            }
            ProjectConfig = _inList;
        }

        public static void AddExtendProject(List<string> _proInfo)
        {
            CheckExtendProjectList(_proInfo);
            ProjectConfig.Add(_proInfo);
        }

        public static void CheckExtendProjectList(List<string> _proInfo)
        {
            string projFile = _proInfo[0];
            string path = Environment.CurrentDirectory + "/" + projFile;
            if (!File.Exists(path))
            {
                throw new Exception(string.Format("path is {0}", path));
            }
        }

        #region Test Code
        //private static string strTest = "\r\nMicrosoft Visual Studio Solution File, Format Version 12.00\r\n# Visual Studio for Mac\r\nProject(\"{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}\") = \"Assembly-CSharp\", \"Assembly-CSharp.csproj\", \"{A2859A56-AB4A-51F9-60BB-1B6FC96E8C70}\"\r\nEndProject\r\nProject(\"{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}\") = \"Assembly-CSharp-firstpass\", \"Assembly-CSharp-firstpass.csproj\", \"{3A1B43A0-276E-34FB-197E-1ACDE9001A0F}\"\r\nEndProject\r\nProject(\"{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}\") = \"Assembly-CSharp-Editor\", \"Assembly-CSharp-Editor.csproj\", \"{2FC59FCE-BCB1-13D3-FB79-5DD1AF79A2AF}\"\r\nEndProject\r\nGlobal\r\n\tGlobalSection(SolutionConfigurationPlatforms) = preSolution\r\n\t\tDebug|Any CPU = Debug|Any CPU\r\n\t\tRelease|Any CPU = Release|Any CPU\r\n\tEndGlobalSection\r\n\tGlobalSection(ProjectConfigurationPlatforms) = postSolution\r\n\t\t{A2859A56-AB4A-51F9-60BB-1B6FC96E8C70}.Debug|Any CPU.ActiveCfg = Debug|Any CPU\r\n\t\t{A2859A56-AB4A-51F9-60BB-1B6FC96E8C70}.Debug|Any CPU.Build.0 = Debug|Any CPU\r\n\t\t{A2859A56-AB4A-51F9-60BB-1B6FC96E8C70}.Release|Any CPU.ActiveCfg = Release|Any CPU\r\n\t\t{A2859A56-AB4A-51F9-60BB-1B6FC96E8C70}.Release|Any CPU.Build.0 = Release|Any CPU\r\n\t\t{3A1B43A0-276E-34FB-197E-1ACDE9001A0F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU\r\n\t\t{3A1B43A0-276E-34FB-197E-1ACDE9001A0F}.Debug|Any CPU.Build.0 = Debug|Any CPU\r\n\t\t{3A1B43A0-276E-34FB-197E-1ACDE9001A0F}.Release|Any CPU.ActiveCfg = Release|Any CPU\r\n\t\t{3A1B43A0-276E-34FB-197E-1ACDE9001A0F}.Release|Any CPU.Build.0 = Release|Any CPU\r\n\t\t{2FC59FCE-BCB1-13D3-FB79-5DD1AF79A2AF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU\r\n\t\t{2FC59FCE-BCB1-13D3-FB79-5DD1AF79A2AF}.Debug|Any CPU.Build.0 = Debug|Any CPU\r\n\t\t{2FC59FCE-BCB1-13D3-FB79-5DD1AF79A2AF}.Release|Any CPU.ActiveCfg = Release|Any CPU\r\n\t\t{2FC59FCE-BCB1-13D3-FB79-5DD1AF79A2AF}.Release|Any CPU.Build.0 = Release|Any CPU\r\n\tEndGlobalSection\r\n\tGlobalSection(SolutionProperties) = preSolution\r\n\t\tHideSolutionNode = FALSE\r\n\tEndGlobalSection\r\nEndGlobal\r\n";
        private static string strSlnName = @"../../../../us_unity/client/client.sln";
        private static string strProjName = @"../../../../us_unity/client/Assembly-CSharp.csproj";

        public static void TestParser()
        {
            //UnitySolution us = new UnitySolution();
            //us.ParseSolutionFile(@"SolutionGeneratorHelper.sln");


            //UnityProject up = new UnityProject();
            //up.ParseProjectFile(@"Test1111.csproj");
            //up.InsertProjectFile(@"../../us_unity/kernel/USClientKernel/USClientKernel.csproj");

            //string strProj = up.Serialize();
            //File.WriteAllText(@"Test1.csproj", strProj, Encoding.UTF8);


            //us.InsertProject(@"../../us_unity/kernel/USClientKernel/USClientKernel.csproj");
            //string strSolu = us.Serialize();

            //File.WriteAllText(@"SolutionGeneratorHelper1.sln", strSolu, Encoding.UTF8);

            ProjectConfig.Add(new List<string> {
                @"../../../../us_unity/kernel/USClientKernel/USClientKernel.csproj",
                ENUM_STR_PROJECT.DEFAULT
            });


            string strSln = File.ReadAllText(strSlnName);
            UnitySolution.SolutionFileHandler(strSlnName, strSln);

            string strProj = File.ReadAllText(strProjName);
            UnitySolution.ProjectFileHandler(strProjName, strProj);

        }
        #endregion
    }
}
